﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Security.Principal;
using System.ServiceModel.DomainServices.Server;
using System.Text;
#endregion

namespace NetBay.Core.BusinessEntities
{
    /// <summary>
    /// DATABASE TABLENAME : r_user
    /// </summary>

    [DataContract]
    public class User : ReturnObject, IIdentity
    {
        #region Private members
        /// <summary>
        /// Database Identifier
        /// </summary>
        private long m_lIdentifier;
        /// <summary>
        /// User Login
        /// </summary>
        private string m_sLogin;
        /// <summary>
        /// User password
        /// </summary>
        private string m_sPassword;
        /// <summary>
        /// User first name
        /// </summary>
        private string m_sFirstName = string.Empty;
        /// <summary>
        /// User last name
        /// </summary>
        private string m_sLastName = string.Empty;
        /// <summary>
        /// User email
        /// </summary>
        private string m_sEmail;
        /// <summary>
        /// List of group where the user is assigned
        /// </summary>
        private Group[] m_lstGroup;
        /// <summary>
        /// List of all user actions 
        /// </summary>
        private List<ActionHistoric> m_lstUserActionHistoric = new List<ActionHistoric>();

        private String m_sLicenseKey;

        private bool m_bCTX_ClientScan;
        private string m_sCTX_ParameterStorage;
        private string m_sCTX_LicenseKey;
        private string m_sCTX_Culture;
        private int m_sCTX_DefaultOrganization;
        private int m_sCTX_DefaultType;
        private string m_sCTX_ParameterStorageThumbnail;
        private bool m_sCTX_DisplayMode;
        private bool m_sCTX_FitToheight;
        private double m_sCTX_ZoomStep;


        [DataMember]
        public long UserId
        {
            get { return m_lIdentifier; }
            set { m_lIdentifier = value; }
        }

        [DataMember]
        public string Login
        {
            get { return m_sLogin; }
            set { m_sLogin = value; }
        }

        [DataMember]
        public string Password
        {
            get { return m_sPassword; }
            set { m_sPassword = value; }
        }

        [DataMember]
        public string FirstName
        {
            get { return m_sFirstName; }
            set { m_sFirstName = value; }
        }
        
        [DataMember]
        public string LastName
        {
            get { return m_sLastName; }
            set { m_sLastName = value; }
        }

        [DataMember]
        public string FullName
        {
            get { return m_sFirstName + " " + m_sLastName; }
            set { }
        }

        [DataMember]
        public string Email
        {
            get { return m_sEmail; }
            set { m_sEmail = value; }
        }

        [Association("GroupList", "Key", "ForeignKey")]
        [Include]
        [DataMember]
        public Group[] GroupList
        {
            get { return m_lstGroup; }
            set 
            { 
                m_lstGroup = value;
                ItemEntity.SetForeignKey(m_lstGroup, Key);
            }
        }

        public List<ActionHistoric> UserActionHistoricList
        {
            get { return m_lstUserActionHistoric; }
            set
            {
                m_lstUserActionHistoric = value;
                ItemEntity.SetForeignKey(m_lstUserActionHistoric, Key);
            }
        }

        [DataMember]
        public String LicenseKey
        {
            get { return m_sLicenseKey; }
            set { m_sLicenseKey = value; }
        }

        /// <summary>
        /// Gets or sets the refresh scan GUID.
        /// </summary>
        /// <value>The refresh scan GUID.</value>
        [DataMember]
        public String RefreshScanGuid { get; set; }

        #region User Context
        [DataMember]
        public bool CTX_ClientScan
        {
            get { return m_bCTX_ClientScan; }
            set { m_bCTX_ClientScan = value; }
        }
       
        [DataMember]
        public string CTX_ParameterStorage
        {
            get { return m_sCTX_ParameterStorage; }
            set { m_sCTX_ParameterStorage = value; }
        }
        
        [DataMember]
        public string CTX_LicenseKey
        {
            get { return m_sCTX_LicenseKey; }
            set { m_sCTX_LicenseKey = value; }
        }
            
        [DataMember]
        public string CTX_Culture
        {
            get { return m_sCTX_Culture; }
            set { m_sCTX_Culture = value; }
        }

        [DataMember]
        public int CTX_DefaultOrganization
        {
            get { return m_sCTX_DefaultOrganization; }
            set { m_sCTX_DefaultOrganization = value; }
        }

        [DataMember]
        public int CTX_DefaultType
        {
            get { return m_sCTX_DefaultType; }
            set { m_sCTX_DefaultType = value; }
        }

        [DataMember]
        public string CTX_ParameterStorageThumbnail
        {
            get { return m_sCTX_ParameterStorageThumbnail; }
            set { m_sCTX_ParameterStorageThumbnail = value; }
        }

        [DataMember]
        public bool CTX_DisplayMode
        {
            get { return m_sCTX_DisplayMode; }
            set { m_sCTX_DisplayMode = value; }
        }

        [DataMember]
        public bool CTX_FitToheight
        {
            get { return m_sCTX_FitToheight; }
            set { m_sCTX_FitToheight = value; }
        }

        [DataMember]
        public double CTX_ZoomStep
        {
            get { return m_sCTX_ZoomStep; }
            set { m_sCTX_ZoomStep = value; }
        }

        public string Name
        {
            get { return FullName; }
        }

        public string AuthenticationType
        {
            get { return "RioAuthentication"; }
        }

        public bool IsAuthenticated { get; set; }

        public bool IsSuperAdmin { get; set; }

        #endregion

        #endregion


        /// <summary>
        /// Default constructor
        /// </summary>
        public User()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FolderType"/> class for error usages
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="applicationException">The application exception.</param>
        public User(string errorMessage, Exception applicationException = null)
            : base(errorMessage, applicationException)
        {
        }

        #region ToString
        /// <summary>
        /// Extend default description of class
        /// </summary>
        /// <returns>description of instance</returns>
        public override string ToString()
        {
            StringBuilder parameters = new StringBuilder("UserId<", 4096);
            parameters.Append(UserId); parameters.Append("> ");
            parameters.Append("Login<"); parameters.Append(Login); parameters.Append("> ");

            return parameters.ToString();
        }
        #endregion
        
        public override object Clone()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets an anonymous user.
        /// </summary>
        /// <returns></returns>
        public static User GetAnonymous()
        {
            return new User { IsAuthenticated = false };
        }
    }

    public abstract class UserDer : User
    {
        #region Abstract Method

        public abstract void SetGroupList();

        #endregion
    }
}
